
{% extends 'base.html' %}

{% block css%}
<style type="text/css">

</style>
{% endblock %}

{% block body%} 
<div class="row">

	<div class="col-md-8">
	<button id="btn_get_all" class="btn btn-success pull-right" style="margin-bottom:10px">
	Get all
	&nbsp;&nbsp;
	<i id="btn_get_all_loading" class="fa fa-refresh fa-spin fa-fw"></i>
	</button>

	<table id="t_dac" class="table table-hover table-condensed form-inline">
		<tr>
			<th>Name</th>
			<th>Unit</th>
			<th>Aval</th>
			<th>Rawval</th>
			<th>Dval</th>
			<th>Operate</th>
		</tr>		
		
		{% for dac_item in dac_items %}
			<tr>
				<td style="font-size:12px">
					{{ dac_item[0] }}
				</td>
				<td>
					{{ dac_item[1] }}
				</td>

				<td>
					<input class="form-control" style="width:120px"  type="text" id="dac_phy_val_{{ dac_item[0] }}">
				</td>

				<td>
					<input class="form-control" style="width:120px; background-color:#eee"  readonly type="text" id="dac_raw_val_{{ dac_item[0] }}">
				</td>

				<td>
					<input class="form-control" style="width:120px; background-color:#eee"  readonly type="text" id="dac_hex_val_{{ dac_item[0] }}">
				</td>

				<td>
					<button class="btn btn-success" onClick="btn_dac_evt_get('{{ dac_item[0] }}')">
					Get
					</button>
					&nbsp;&nbsp;
					<button class="btn btn-primary" onClick="btn_dac_evt_set('{{ dac_item[0] }}')">
					Set
					</button>
				</td>
			</tr>
			{% endfor %}
	</table>
	</div>

</div>
{% endblock %}

{% block js%} 
{{ super() }}
<script type="text/javascript">

	function btn_dac_evt_get(item)
	{
		$.post("api/get_dac",
        {
            item:item,
        },
        function(data,status){
            console.log("data: " + data);
			cells = data.split(",");

			$("#dac_phy_val_" + item).val(cells[0]);
			$("#dac_hex_val_" + item).val(cells[1]);
			$("#dac_raw_val_" + item).val(cells[2]);

			// save to session
			sessionStorage.setItem("dac_phy_val_" + item, cells[0]);
			sessionStorage.setItem("dac_hex_val_" + item, cells[1]);
			sessionStorage.setItem("dac_raw_val_" + item, cells[2]);
        });
	}

	function btn_dac_evt_get1(item)
	{
		$.post("api/get_dac",
        {
            item:item,
        },
        function(data,status){
            console.log("data: " + data);
			cells = data.split(",");

			// $("#dac_phy_val_" + item).val(cells[0]);
			$("#dac_hex_val_" + item).val(cells[1]);
			$("#dac_raw_val_" + item).val(cells[2]);

			// save to session
			sessionStorage.setItem("dac_phy_val_" + item, cells[0]);
			sessionStorage.setItem("dac_hex_val_" + item, cells[1]);
			sessionStorage.setItem("dac_raw_val_" + item, cells[2]);
        });
	}

	function btn_dac_evt_set(item)
	{
		dac_phy_val = $("#dac_phy_val_" + item).val();
		if(dac_phy_val=="")
		{
			alert("Invalid value.");
			return false;
		}

		$.post("api/set_dac",
        {
			item:item,
			dac_phy_val:dac_phy_val,
        },
        function(data,status){
        	toastr["success"]("Command successfully issued.");
            console.log("data: " + data);
			// btn_dac_evt_get(item);
			btn_dac_evt_get1(item);
        });
	}

	function read_session_data()
	{
		// alert('read_session_data');
		// dac_names = ['DRIVER_VGC_XI', 'DRIVER_VGC_YI', 'DRIVER_VGC_XQ', 'DRIVER_VGC_YQ', 'MCU1_TX_VOA_X', 'MCU1_TX_VOA_Y', 'DRIVER_VT', 'MCU1_DAC_TX_PN_BIAS', 'COSA_VOFE_FB', 'MCU2_PH_XIP', 'MCU2_PH_XQP', 'MCU2_PH_XP', 'MCU2_PH_YIP', 'MCU2_PH_YQP', 'MCU2_PH_YP', 'MCU2_PH_XIN', 'MCU2_PH_XQN', 'MCU2_PH_XN', 'MCU2_PH_YIN', 'MCU2_PH_YQN', 'MCU2_PH_YN', 'DAC_TIA_VOA_XI', 'DAC_TIA_VOA_XQ', 'DAC_TIA_VOA_YI', 'DAC_TIA_VOA_YQ', 'DAC_RX_VOA_X', 'DAC_RX_VOA_Y', 'DAC_RX_IQ_PT_X1', 'DAC_RX_IQ_PT_X2', 'DAC_RX_IQ_PT_Y1', 'DAC_RX_IQ_PT_Y2', 'ABC_VGA_R'];

		dac_names = [
                  'DRIVER_VGC_XI', 'DRIVER_VGC_YI',
                  'DRIVER_VGC_XQ', 'DRIVER_VGC_YQ',
                  'MCU1_TX_VOA_X', 'MCU1_TX_VOA_Y',
                  'DRIVER_VT', 'MCU1_DAC_TX_PN_BIAS',
                  'COSA_VOFE_FB', 'P0V55_DSP_FB',
                  'MCU2_TXIH1_AC', 'MCU2_TXIH1_DC',
                  'MCU2_TXQH1_DC', 'MCU2_TYIH1_DC',
                  'MCU2_TYQH1_DC', 'MCU2_TXQH1_AC',
                  'MCU2_TYIH1_AC', 'MCU2_TYQH1_AC',
                  'MCU2_TXPH1_DC', 'MCU2_TYPH1_DC',
                  'MCU2_TXPH1_AC', 'MCU2_TYPH1_AC',
                  'DAC_TIA_VOA_YI', 'DAC_TIA_VOA_XI',
                  'DAC_TIA_VOA_YQ', 'DAC_TIA_VOA_XQ',
                  'DAC_RX_VOA_X', 'DAC_RX_VOA_Y',
                  'DAC_RX_IQ_PH_X', 'DAC_RX_IQ_PH_Y',
                  'ABC_VGA_R'
            ];

		for(idx = 0; idx < dac_names.length; idx++)
		{
			$("#dac_phy_val_"+dac_names[idx]).val(sessionStorage.getItem("dac_phy_val_"+dac_names[idx]));
			$("#dac_hex_val_"+dac_names[idx]).val(sessionStorage.getItem("dac_hex_val_"+dac_names[idx]));
			$("#dac_raw_val_"+dac_names[idx]).val(sessionStorage.getItem("dac_raw_val_"+dac_names[idx]));
		}
	}


	function btn_get_all()
	{
		$("#btn_get_all_loading").show();
		$.post("api/get_dac_all",
        {
        },
        function(data,status){
            console.log(data);
            d = JSON.parse(data);
            $.each(d,function(index,value){
                // $("#analog1_val_" + index).html(value);	

                cells = value.split(',');
				$("#dac_phy_val_" + index).val(cells[0]);
				$("#dac_hex_val_" + index).val(cells[1]);
				$("#dac_raw_val_" + index).val(cells[2]);

				// save to session
				sessionStorage.setItem("dac_phy_val_" + index, cells[0]);
				sessionStorage.setItem("dac_hex_val_" + index, cells[1]);
				sessionStorage.setItem("dac_raw_val_" + index, cells[2]);

				$("#btn_get_all_loading").hide();
            });
        });
	}


	$(document).ready(function(){
        toastr.options = {
            "closeButton": true,
            "positionClass": "toast-top-center"
        } 
         
		$("#btn_get_all_loading").hide();
		read_session_data();

		//var tr_num = $('#t_dac tr').size();
		// $('#btn_get_all').click(function(){
		// 	var item = ''
		// 	$('#t_dac tr').each(function(i, val){
		// 		$(this).children('td').each(function(j){ 
		// 			//alert("绗?+(i)+"琛岋紝绗?+(j)+"涓猼d鐨勫€硷細"+$(this).text());
		// 			if(j == 0){
		// 				item = $.trim($(this).text());
		// 				//alert(i + ": " + item)
		// 				// alert(item);
		// 				btn_dac_evt(item);
		// 				return false;
		// 			}
		// 		});
		// 	});
		// });

		// get all --new method
		$('#btn_get_all').click(function(){
			btn_get_all();
		});


	});
</script>
{% endblock %}